defmodule House.Web.PhotoControllerTest do
  use House.Web.ConnCase

  alias House.BasicData

  @create_attrs %{kind: 42, url: "some url"}
  @update_attrs %{kind: 43, url: "some updated url"}
  @invalid_attrs %{kind: nil, url: nil}

  def fixture(:photo) do
    {:ok, photo} = BasicData.create_photo(@create_attrs)
    photo
  end

  test "lists all entries on index", %{conn: conn} do
    conn = get conn, photo_path(conn, :index)
    assert html_response(conn, 200) =~ "Listing Photos"
  end

  test "renders form for new photos", %{conn: conn} do
    conn = get conn, photo_path(conn, :new)
    assert html_response(conn, 200) =~ "New Photo"
  end

  test "creates photo and redirects to show when data is valid", %{conn: conn} do
    conn = post conn, photo_path(conn, :create), photo: @create_attrs

    assert %{id: id} = redirected_params(conn)
    assert redirected_to(conn) == photo_path(conn, :show, id)

    conn = get conn, photo_path(conn, :show, id)
    assert html_response(conn, 200) =~ "Show Photo"
  end

  test "does not create photo and renders errors when data is invalid", %{conn: conn} do
    conn = post conn, photo_path(conn, :create), photo: @invalid_attrs
    assert html_response(conn, 200) =~ "New Photo"
  end

  test "renders form for editing chosen photo", %{conn: conn} do
    photo = fixture(:photo)
    conn = get conn, photo_path(conn, :edit, photo)
    assert html_response(conn, 200) =~ "Edit Photo"
  end

  test "updates chosen photo and redirects when data is valid", %{conn: conn} do
    photo = fixture(:photo)
    conn = put conn, photo_path(conn, :update, photo), photo: @update_attrs
    assert redirected_to(conn) == photo_path(conn, :show, photo)

    conn = get conn, photo_path(conn, :show, photo)
    assert html_response(conn, 200) =~ "some updated url"
  end

  test "does not update chosen photo and renders errors when data is invalid", %{conn: conn} do
    photo = fixture(:photo)
    conn = put conn, photo_path(conn, :update, photo), photo: @invalid_attrs
    assert html_response(conn, 200) =~ "Edit Photo"
  end

  test "deletes chosen photo", %{conn: conn} do
    photo = fixture(:photo)
    conn = delete conn, photo_path(conn, :delete, photo)
    assert redirected_to(conn) == photo_path(conn, :index)
    assert_error_sent 404, fn ->
      get conn, photo_path(conn, :show, photo)
    end
  end
end
